Repository navigation
feat(codegen-ts): run business rules in the generated service layer - #642
Merged
Merged
Conversation
…its history The install of @metaobjectsdev/cli measures 51.5 MB against a 51.0 MB budget: feature growth since #622, measured late because hosted CI does not run ts-slow. Ruling (Firstmate, 2026-11-22): raise to the measurement plus 0.2 MB and keep the declaration maps. check-install-size.mjs accepts a raise only when the newest history entry records it (from main's budget, to the new one, with a reason and a ruling), and it falls back to origin/main when there is no local main, so the never-rises rule no longer skips such a checkout.
…rated service (L-TS-5) The service generator now writes a command per transition that derives one and a shell per declared command and query, each running ADR-0066 section 6's checks in one transaction: the row (404), the permission point (403 forbidden), the state (409 invalid_transition), the hand-written body, the guarded update (409 invalid_transition or transition_condition_failed) and the event hook. The generic update refuses a changed lifecycle field (409 state_managed). What an app writes attaches through generated interfaces passed to create<Pkg>Service(deps): the policy, the operation bodies, optional hooks on the derived writes and the write-event hook. Any of them may refuse, which rolls the write back. The actor is an app-defined type parameter, passed as opts.actor and resolved by the Fastify transport's getActor(request). rules.lifecycle.create, rules.lifecycle.commands and rules.permissions.names: require now act on the generated side. New shared API tests (fixtures/api-contract-conformance/rules/, 17 scenarios with three settings variants) run on the TypeScript service lane in both layouts, and the service compile gate builds the rules model in every dialect. ADR-0066's "no effect yet" is removed, and ADR-0067, the business-rules and API-contract docs and the CHANGELOG say what the service now does.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What Changed
servicegenerator (codegen-ts) now emits a command for each transition that derives one (rules.lifecycle.commands) and a shell for each declared command and query. Each shell runs ADR-0066 §6's checks in one transaction, in order: row (404), permission (403forbidden), state (409invalid_transition), hand-written body, guarded update (409invalid_transitionortransition_condition_failed), then the event hook. A changed lifecycle field on the generic update answers 409state_managed.rules.lifecycle.createandrules.permissions.names: requiretake effect, and generation refuses a command with@transitionsthat addresses no row by key, a duplicate method and path, and arequirepoint with no@permission. Policy, bodies, hooks and events reach the service through generated interfaces passed tocreate<Pkg>Service(deps), and any of them may refuse, which rolls the write back.runtime-ts/serviceaddsrules.ts, which holds the runtime helpers the shells call (checkPermission,checkState,makeTransition,checkStateManaged,checkCreateState,emitWrite,handlerContext,refuseBusiness).OpOptions,ServiceDeps, the service interface and the factory take an app-defined actor typeAthat defaults tounknown, so existing callers compile unchanged. The Fastify transport resolves the actor through an optionalgetActor(request).fixtures/api-contract-conformance/rules/(generated lane, TypeScript only) with three settings variants covering 403, the three 409 codes, a declared error, a derived and a declared command, a query, a public operation, and rollback when a hook or event hook refuses. The CLI rules-inert test gains aSERVICE_LAYERcategory so service generators may change files for rules vocabulary. The install-size budget is raised by ruling, recorded inscripts/install-size-budget.json. Docs and ADR-0066/0067 are updated to match, and the CHANGELOG records the change.Validation
Risk Assessment
✅ Low: The change is bounded to the opt-in service generators, a new runtime module and a new shared corpus. The check order and the guarded update match ADR-0066 §6, every intent requirement is present, and I found no wrong-result path.
Testing
Baseline ci-local --affected passed at the target commit and was not re-run. Drove the generated TypeScript service live: the existing rules api-contract test boots the emitted service and transport over HTTP against real Postgres testcontainers, and all 34 runs (17 scenarios, 2 layouts) passed, covering lifecycle create under initial and any, state_managed, 403 forbidden, invalid_transition, transition_condition_failed, declared errors, derived and declared commands, the public query, and rollback on hook and event refusal. Also ran the worktree's codegen on disposable bad models: generation refuses a require-names point and a command with no row addressed by key, and generates cleanly for the control. Not driven live: the two-routes-on-one-method-and-path refusal. Overall result passes.
Evidence: Rules api-contract run log (34 pass)
Evidence: Rules api-contract junit report (scenario names, both layouts)
Evidence: Generation refusal and control transcript
Gate and scope
fff31a820, the install-size budget raise by Firstmate's ruling, ridden on this branch deliberately.agent-context-shipped/tree with their generators (no hand edits); the agent-context tests pass (130/130).ERR_STORE_NO_INTERACTIVE_TX, sync SQLite and D1 (L-TS-6); the init flag, theverifyadvisory for a lifecycle with no service,gen --list(L-TS-7); moving reserved-name refusals to the generator (T6b). The other ports' services and their lanes of the newrules/corpus are each port's own 1.3.0 work.Pipeline
Updates from git push no-mistakes
✅ **Intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
server/typescript/packages/codegen-ts/src/templates/service-file.ts:450- A declared command or query with an@inputRefvalidates its input before the permission point. ADR-0066 §6 lists load row, permission, state, body, and does not mention input validation. Validation first is needed because the policy receives the validated input. The cost is that a caller the policy would refuse gets 400 validation detail before 403. The generated doc comment states the order, so this is a recorded choice, not a defect. Consider one line in ADR-0066 §6 saying validation precedes the permission point on operations that take an input.✅ **Test** - passed
✅ No issues found.
scripts/ci-local.sh --affected --strict-toolchainsBaseline (configured): scripts/ci-local.sh --affected --strict-toolchains, passed at target commit 5d18b7ef7e (not re-run; no source changed in this turn)cd server/typescript/packages/integration-tests && bun test test/api-contract-rules.test.ts (34 pass, 0 fail; junit list shows 17 scenarios x 2 output layouts incl. settings/create-any, settings/commands-all, settings/commands-none)Scenario 1 and 2 evidence = the same run: create-starts-in-initial-state, create-in-any-state, state-managed-update, declared-permission-is-asked, derived-command-forbidden, public-command-needs-no-actor, invalid-transition, transition-condition-failed, declared-command-transitions, derived-command-moves-the-row, claimed-transition-has-no-command, claimed-transition-has-a-command, no-derived-commands, public-queryScenario 3 evidence = declared-error-rolls-back, hook-refusal-rolls-back, event-refusal-rolls-back in the same runAdversarial generation refusals: disposable driver under the scratch directory calling the worktree's codegen-ts runGen with serviceFile + serviceRoutesFile on 4 model variants: control (generates ok), rules.permissions.names=require (refused, names derived acme::purchasing.PurchaseRequest.submit), decide input without key borrow with path lacking placeholder (refused: addresses no row by key), decide with key-less input and placeholder path (refused by loader ERR_BINDING_PATH_MISMATCH). Transcript in gen-refusals.logCleanup: removed the scratch driver dir and temp pid file; git status clean in worktree; no background jobs of mine running (a bun test vocabulary-checks run belongs to another session, untouched); the four postgres and Ryuk containers started about 4 minutes ago are not verifiably mine and were not stopped🔧 **Document** - 1 issue found → auto-fixed ✅
agent-context/skills/metaobjects-audit/references/calibration.md:37- agent-context/skills/metaobjects-audit/references/calibration.md:37 and capability-checklist.md:369 still say the declared-API and lifecycle vocabulary has 'no generator emits from them yet'. False for TypeScript after this change. Not edited: the run intent says agent-context/*.md must not be touched. Fix is a one-line reword in each file, once Doug allows it.🔧 Fix applied.
✅ Re-checked - no issues remain.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.